import axios from 'axios'
// 配置默认的host,假如你的API host是：http://api.htmlx.club

// 添加请求拦截器
axios.interceptors.request.use(config => {
    if (config.method === 'post'||config.method === 'put') {
        // config.data = qs.stringify(config.data)
        config.headers['Content-Type'] = 'application/json; charset=UTF-8'
        return config
    }

    return config
}, error => {
    console.log(error);
    // 对请求错误做些什么
    return Promise.reject(error)
});

// // 添加响应拦截器
axios.interceptors.response.use(response => {
    console.log('拦截器->' + response);
    return response.data;
}, error => {
    let result;
    // 对响应错误做点什么
    if (
        error.response.data.status === 404
        || error.response.data.status === 500
    ) {
        result = getMessage(404, '服务器出了小差！');
    }
    console.log('拦截器【失败】->' + result);
    return result;
});

function getMessage(code, msg) {
    let result = {};
    result.code = code;
    result.msg = msg;
    // response.data = {};
    // response.data.result = result;
    return result;
}